// 接口排序 (表格) ===============================================================
import { FCouponTypesLabel } from "@/filters";
import { _userStore } from "@/store/config";

export interface TypeTableSort {
    order: string
    prop: string
}

export interface TypeSortParams {
    order_by: string
    sort: string
}

export const getTableSortOrder = (order: string = '', prop?: string): TypeSortParams => {
    const params: TypeSortParams = {
        order_by: '',
        sort: '',
    };
    if (order) {
        params.order_by = prop as string;
        if (order === 'ascending') params.sort = 'asc';
        if (order === 'descending') params.sort = 'desc';
    }
    return params;
}

// 优惠券列表 数据处理 ===============================================================
export const handlerCouponListApi = (row: ObjValueAny) => {
    if (row.item?.id) {
        row.miniapp_code = row.item.miniapp_code;
    }
    row.verifyNum = (+row.verify_num || 0) + (+row.online_verify_num || 0);
    row.operator = handlerOperator(row.created_user || {});
    row.couponType = FCouponTypesLabel[row.soule];
}
// 操作人
export const handlerOperator = (row: ObjValueAny): string => {
    let value: string = '';
    if (row) {
        const { username, email, phone } = row;
        if (username || email || phone) {
            if (username) {
                value = username + (email || phone || '');
            } else if (email || phone) {
                value = email || phone
            }
        }
    }
    return value;
}
// 操作按钮显示
export const getCouponTableBtn = (row: ObjValueAny, valuable?: number) => {
    const isShowLink: boolean = (row.status === 1 || row.status === 2) && (row.purpose === 1 || row.purpose === 7);// 显示获取链接按钮
    const isPaidMemberGift: boolean = row.purpose === 14;// 付费会员礼
    if (valuable === 1) {
        // 有价券 ======================================================================
        row.showLinkBtn = isShowLink;// 显示获取链接按钮
    } else {
        // 免费券 ======================================================================
        // 显示通过按钮
        row.passBtn = row.status === 0;
        // 显示驳回按钮
        row.rejectBtn = row.passBtn && row.origin === 2;
        // 显示上架按钮
        row.showUpBtn = row.status === 4 && row.used !== 1 && !isPaidMemberGift;
        // 显示下架按钮
        row.showDownBtn = ((row.status === 1 && (row.purpose === 1 || row.purpose === 7)) || (row.status === 8 && row.purpose === 1)) && !isPaidMemberGift;
        // 显示修改分发量按钮
        row.showEditNumBtn = row.status === 1 && (row.used !== 1 || (row.purpose !== 9 && row.purpose !== 13));
        // 显示编辑按钮
        row.showEditBtn = ((row.status === 2 || row.status === 0 || row.status === 4) && row.used !== 1) || (_userStore.isSuperManage && !row.showDownBtn);
        // 显示获取链接按钮
        row.showLinkBtn = (isShowLink || (row.status === 8 && row.purpose === 1) || row.status === 4) && row.used !== 1 && !isPaidMemberGift;
    }
    // 显示报表按钮
    row.showReportBtn = row.status !== 2;
}